<template>
  <div>
    <a @click="fabutwitter(item.id)" style="display: inline"></a>
    <el-dialog title="提示" :visible.sync="visible" width="30%">
      <span>确定发送至Twitter吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="shareTwitter" :loading="abcsdd"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      abcsdd:false,
      visible:false,
      goodsId:0,
      uuid:'',
      goods: [],
    };
  },
  methods: {
    fabutwitter(id){
      // 这个ID为商品ID
      this.goodsId=id
      // 打开对话框选择是否分享到Twitter
      this.visible=true
    },
    // 分享到推特
    shareTwitter() {
      let that = this
      // abcsdd为样式的标识
      that.abcsdd=true
      // 将商品ID存进内存
      localStorage.setItem('oauth_goods_id',that.goodsId)
      //不在这里分享Twitter，这里是授权，真正分享是在Twitter.vue页面
      share_goods_html2().then((res)=>{
        that.visible=false
        that.abcsdd=false
        // 成功返回授权地址res.data.url和随机码uuid
        localStorage.setItem('oauth_uuid',res.data.uuid)
        window.location.href=res.data.url
      }).catch((err)=>{
        that.abcsdd=false
        that.visible=false
        that.$message({
            message:'Failed to post to twitter, please try again',
            type: 'error'
        })
      })
    },
  }, 
};
</script>
